package com.zuihai.app.mvp.ui.widget.comment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.zuihai.app.R;
import com.zuihai.app.mvp.ui.dialog.OneButtonDialog;
import com.zuihai.app.mvp.ui.utils.DividerGridItemDecoration;

/**
 * 底部弹出评论框 用于RecycleView 中的Item
 */
@SuppressWarnings("unused")
public class BottomSheetPictureTwoBar {
    private View mRootView;
    private EditText mEditText;

    private Context mContext;
    private TextView mBtnCommit;
    private MyBottomDialog mDialog;
    private RecyclerView mRecyclerView;

    private boolean isFirstMax = true;

    // private EmojiView mEmojiView;
    private BottomSheetPictureTwoBar(Context context) {
        this.mContext = context;
    }

    @SuppressLint("InflateParams")
    public static BottomSheetPictureTwoBar delegation(Context context) {
        BottomSheetPictureTwoBar bar = new BottomSheetPictureTwoBar(context);
        bar.mRootView = LayoutInflater.from(context).inflate(R.layout.layout_bottom_sheet_comment_picture_bar, null, false);
        bar.initView();
        return bar;
    }

    private void initView() {
        mEditText = (EditText) mRootView.findViewById(R.id.et_comment);
        mBtnCommit = (TextView) mRootView.findViewById(R.id.btn_comment);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        initRecycler();
        KeyboardUtils.showSoftInput(mEditText);

        mBtnCommit.setEnabled(false);
        mDialog = new MyBottomDialog(mContext);
        mDialog.setContentView(mRootView);
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {

                KeyboardUtils.hideSoftInput(mContext, mEditText);

//                mFrameLayout.setVisibility(View.GONE);
            }
        });

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mEditText.getText().length() > 0) {
                    // if (mImages.size()>0&&mEditText.getText().length()>0){
                    if (s.length() > 1000) {

                        if (isFirstMax) {
                            OneButtonDialog errorDialog = new OneButtonDialog.Build(mContext)
                                    .setConfirmListener(new OneButtonDialog.OnConfirmListener() {
                                        @Override
                                        public void onClick(OneButtonDialog dialog, View v) {
                                            dialog.dismiss();
                                        }
                                    })
                                    .setLogoResuorce(R.mipmap.zhanwei)
                                    .setBottonText("知道了")
                                    .setTextResource("评论最大字数限制1000字")
                                    .create();
                            errorDialog.show();
                            isFirstMax = false;
                        }


                        mBtnCommit.setEnabled(false);
                    } else {
                        mBtnCommit.setEnabled(true);
                        isFirstMax = true;
                    }
                } else {
                    mBtnCommit.setEnabled(false);
                }
            }
        });
    }

    private void initRecycler() {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
        mRecyclerView.setLayoutManager(layoutManager);
        DividerGridItemDecoration itemDecoration = new DividerGridItemDecoration(mContext, R.drawable.release_image_divider_shape);
        mRecyclerView.addItemDecoration(itemDecoration);

    }

    public void appendText(String text) {
        if (!TextUtils.isEmpty(text)) {
            mEditText.append(text);
        }
    }

    public void show(String hint) {
        mDialog.show();
        if (!"添加评论".equals(hint)) {
            mEditText.setHint(hint + "");
        }

      /*  mRootView.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                //KeyboardUtils.showSoftInput(mEditText);
            }
        }, 50);*/
    }


    public void setCommitListener(View.OnClickListener listener) {
        mBtnCommit.setOnClickListener(listener);
    }



    public void handleSelectFriendsResult(Intent data) {
        String names[] = data.getStringArrayExtra("names");
        if (names != null && names.length > 0) {
            String text = "";
            for (String n : names) {
                text += "@" + n + " ";
            }
            mEditText.getText().insert(mEditText.getSelectionEnd(), text);
        }
    }


}
